package com.gzz.orchid.common.boot.mybatis.dialect;

import com.gzz.orchid.common.core.model.Pagination;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.session.RowBounds;

/**
 * Oracle方言
 *
 * @author higmd
 */
public class OracleDialect implements IDialect {
    @Override
    public void afterAll() {
    }

    @Override
    public String rebuildSql(BoundSql boundSql) {
        return " select t.* from ( select a.*, ROWNUM as rowno from (" + boundSql.getSql() + ") a where  ROWNUM <= ? ) t where t.rowno > ?";
    }

    @Override
    public String rebuildSql(String sql, Pagination pager) {
        int pageSize = (pager.getTotal() - pager.getOffset()) > pager.getSize() ? pager.getSize() : (pager.getTotal() - pager.getOffset());
        return " select t.* from ( select a.*, ROWNUM as rowno from (" + sql + ") a where  ROWNUM <= "
                + (pager.getOffset() + pageSize) + ") t where t.rowno > " + pager.getOffset();
    }

    @Override
    public String rebuildSql(String sql, int offset, int pageSize) {
        return " select t.* from ( select a.*, ROWNUM as rowno from (" + sql + ") a where  ROWNUM <= "
                + (offset + pageSize) + ") t where t.rowno > " + offset;
    }
}